var fetch = require('@system.fetch')
import storage from '@system.storage'

var API_ROOT = 'http://www.wanandroid.com/'

var headers = {}

function realFetch(url, data = null, method = 'get') {
    console.log("index realFetch =>> ")

    console.log('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━')
    let baseUrl = API_ROOT + url;
    console.log('┃ url: ', baseUrl)
    console.log('┃ method: ', method)
    console.log('┃ data: ', JSON.stringify(data))
    console.log('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━')

    return new Promise((resolve, reject) => {
        fetch.fetch({
            url: API_ROOT + url,
            data: data,
            header: headers,
            method: method,
            success: function (data) {
                // console.log(JSON.parse(data.data).data)
                // console.log("realFetch data = ", JSON.stringify(data))
                resolve(data)
            },
            fail: function (data, code) {
                reject(data)
            }
        })
    })
}

function getAuth() {
    console.log("index getAuth =>> ")
    return new Promise((resolve, reject) => {
        storage.get({
            key: 'auth',
            success: function (data) {
                headers.Cookie = data
                resolve(true)
            },
            fail: function (data, code) {
                resolve(false)
            }
        })
    })

}

function withAuth(url, data = null, method = 'get', canSkip = false) {
    console.log("index withAuth =>> ")
    return getAuth().then((auth) => {
        console.log("index withAuth =>> result = ", auth)
        return getAuth().then((auth) => {
            if (auth || canSkip) {
                return realFetch(url, data, method)
            } else {
                return new Promise((resolve, reject) => {
                    reject('请先登录！')
                })
            }
        })
    })
}

/**
 * post请求
 * @param url
 * @param data
 * @param config
 * @returns {*}
 */
function post(url, data = null, config = {}) {
    console.log("index post =>> ")
    if (config.withAuth) {
        return withAuth(url, data, 'post', config.canSkip)
    } else {
        return realFetch(url, data, 'post')
    }
}

/**
 * get请求
 * @param url
 * @param data
 * @param config
 * @returns {*}
 */
function get(url, data = null, config = {}) {
    console.log("index get =>> ")
    if (config.withAuth) {
        console.log("index get =>> 需要验证")
        return withAuth(url, data, 'get', config.canSkip)
    } else {
        console.log("index get =>> 不需要验证")
        return realFetch(url, data, 'get')
    }
}

export default {
    /**
     * 获取首页banner
     * @param sucess
     * @param fail
     * @returns {*}
     */
    getBnnerList(sucess, fail) {
        console.log("index getBnnerList =>> ")
        return get("banner/json", null)
    },

    /**
     * 获取首页文章列表
     */
    getArticle(page) {
        console.log("index getArticle =>> ")
        return get("article/list/" + page + "/json", null, {
            withAuth: true,
            canSkip: true
        })
    },

    /**
     * 获取体系分类
     */
    getClassifyList() {
        console.log("index getClassifyList =>> ")
        return get("tree/json", null)
    },

    /**
     * 根据分类获取文章列表
     */
    getArticleByClassify(page, cid) {
        console.log("index getArticleByClassify =>> ")
        return get("article/list/" + page + "/json?cid=" + cid, null, {
            withAuth: true,
            canSkip: true
        })
    },

    /**
     * 登录
     * @param params
     */
    login(params) {
        console.log("index login =>> ")
        return post("user/login", params)
    },
    /**
     * 注册
     */
    register(params) {
        console.log("index register =>> ")
        return post("user/register", params)
    },


    /**
     * 获取收藏的文章列表
     * @param page
     */
    getCollectAtricleList(page) {
        console.log("index getCollectAtricleList =>> ")
        return get('lg/collect/list/' + page + '/json', null, {withAuth: true})
    },

    /**
     * 收藏文章
     * @param id
     */
    collectArticle(id) {
        console.log("index collectArticle =>> ")
        return post('lg/collect/' + id + '/json', null, {withAuth: true})
    },

    /**
     * 取消文章收藏
     * @param id
     */
    unCollectArticle(id) {
        console.log("index unCollectArticle =>> ")
        return post('lg/uncollect_originId/' + id + '/json', null, {withAuth: true})
    },
    /**
     * 从收藏列表取消收藏
     * @param id
     */
    unCollectArticleByOriginId(id, originId) {
        console.log("index unCollectArticleByOriginId =>> ")
        return post('lg/uncollect/' + id + '/json', {originId: originId}, {withAuth: true})
    }
};
